package com.haoting.blogservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.haoting.blogservice.entity.Collection;
import com.haoting.blogservice.entity.Support;
import com.haoting.blogservice.service.CollectionService;
import com.haoting.commonutils.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tingting
 * @since 2020-12-21
 */
@RestController
@RequestMapping("/blogservice/collection")
//@CrossOrigin
public class CollectionController {

    @Autowired
    CollectionService collectionService;
    @PostMapping("collectionblog")
    public CommonResult collectionblog(@RequestBody Collection collection){

        collectionService.save(collection);

        return CommonResult.ok();
    }

    @PostMapping("collectionor")
    public CommonResult collectionor(@RequestBody Collection collection){
        QueryWrapper<Collection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("blog_id",collection.getBlogId())
                .eq("consumer_id",collection.getConsumerId());
        int count = collectionService.count(queryWrapper);
        if(count>=1){
            return CommonResult.ok().data("exist","true");
        }else {
            return CommonResult.error().data("exist","false");
        }

    }


    @DeleteMapping("delcollection")
    public CommonResult delcollection(@RequestBody Collection collection){

        QueryWrapper<Collection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("blog_id",collection.getBlogId())
                .eq("consumer_id",collection.getConsumerId());
        collectionService.remove(queryWrapper);
        return CommonResult.ok();
    }
}

